﻿using System;
using System.Reflection;

namespace LsTool
{
    /// <summary>
    /// 反射操作
    /// </summary>
    public static class TypeCom
    {
        /// <summary>
        /// 反射获取字段名
        /// </summary>
        /// <param name="obj">类型</param>
        /// <param name="str">值</param>
        /// <returns></returns>
        public static string GetPropertyName<T>(T obj, string str)
        {
        
            Type t = obj.GetType();

            foreach (PropertyInfo pi in t.GetProperties())
            {
                if (pi.PropertyType.Name.ToLower() == "string")
                {
                    object val = pi.GetValue(obj, null);//用pi.GetValue获得值

                    if ((string)val == str)
                    {
                        return pi.Name;//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
                    }
                }
            }
            return string.Empty;
        }
    }
}